﻿(function () {

    //angular module
    var myApp = angular.module('app', []);

    //test controller
    myApp.controller('myController', function ($scope, $filter, $http) {

        $('#ajax_loader').show();
        $http.get('/api/categoryapi/get').success(function (data) {
            if (data) {
                if ($('.categoryId-hidden').val()) {
                    $scope.myCategory = $scope.findObject($('.categoryId-hidden').val(), data);
                } else {
                    $scope.myCategory = data[0];
                }
                $scope.categories = data;
            }
            $('#ajax_loader').hide();
        })
         .error(function () {
             $('#ajax_loader').hide();
         });
        

        $http.get('/api/locationapi/get').success(function (data) {
            if (data) {
                if ($('.location-hidden').val()) {
                    $scope.myLocation = $scope.findObject($('.location-hidden').val(), data);
                } else {
                    $scope.myLocation = data[0];
                }
                $scope.locations = data;
            }
            $('#ajax_loader').hide();
        })
       .error(function () {
           $('#ajax_loader').hide();
       });

        $scope.findObject = function (id, data) {
            var entity;
            $.each(data, function (index, obj) {
                if (obj.id == id) {
                    entity = obj;
                    return;
                }
            });
            return entity;
        };


        $('.submit').click(function () {
            if ($scope.myCategory) {
                $('.categoryId-hidden').val($scope.myCategory.id);
            }
            if ($scope.myLocation) {
                $('.location-hidden').val($scope.myLocation.id);
            }
        });
    });

    bkLib.onDomLoaded(function () { nicEditors.allTextAreas() });
})();

